package ydj.cloud.example.common.web;

public class CommonResultVO<T> {

    /**
     * code 码
     */
    private long code;

    /**
     * 匹配 code 码的详细说明
     */
    private String message;

    /**
     * 返回的数据体
     */
    private T data;

    protected CommonResultVO(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 操作成功返回结果集
     * @param data data
     * @param <T>
     * @return 返回结果集对象
     */
    public static <T> CommonResultVO<T> success(T data) {
        return new CommonResultVO<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 操作成功返回结果集
     * @param data data
     * @param message message
     * @param <T>
     * @return 返回结果集对象
     */
    public static <T> CommonResultVO<T> success(T data, String message) {
        return new CommonResultVO<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 操作失败
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> failed(IErrorCode errorCode) {
        return new CommonResultVO<>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 操作失败
     * @param errorCode
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> failed(IErrorCode errorCode, String message) {
        return new CommonResultVO<>(errorCode.getCode(), message, null);
    }

    /**
     * 操作失败
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> validateFailed(String message) {
        return new CommonResultVO<>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> unauthorized(T data) {
        return new CommonResultVO<>(ResultCode.UNZUTHORIZED.getCode(), ResultCode.UNZUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> forbidden(T data) {
        return new CommonResultVO<>(ResultCode.FORBIDOEN.getCode(), ResultCode.FORBIDOEN.getMessage(), data);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
